/*
* @Author: xfy
* @Date:   2017-08-24 12:13:17
* @Last Modified by:   xfy
* @Last Modified time: 2017-09-14 17:25:17
*/
var webpack = require('webpack');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var HtmlWebpackPlugin = require('html-webpack-plugin');

//环境变量配置. dev / online  线上/开发都可
var WEBPACK_ENV = process.env.WEBPACK_ENV || 'dev';

//获取html-webpack-plugin参数的方法
var getHtmlConfig = function(name, title){
    return {
        template    : './src/view/' + name +'.html',
        filename    : 'view/'+ name +'.html',
        favicon     : './favicon.ico',
        title       : title,
        inject      : true,
        hash        : true,
        chunks      : ['common', name]
    }
};
var config = {
    entry : {
        'common'                : ['./src/page/common/index.js'],
        'index'                 : ['./src/page/index/index.js'],
        'list'                  : ['./src/page/list/index.js'],
        'user-login'            : ['./src/page/user-login/index.js'],
        'user-register'         : ['./src/page/user-register/index.js'],
        'user-pass-reset'       : ['./src/page/user-pass-reset/index.js'],
        'user-pass-update'      : ['./src/page/user-pass-update/index.js'],
        'user-center'           : ['./src/page/user-center/index.js'],
        'user-center-update'    : ['./src/page/user-center-update/index.js'],
        'result'                : ['./src/page/result/index.js'],
        'about'                 : ['./src/page/about/index.js'],
        'admin-user-login'      : ['./src/page/admin-page/login/index.js'],
        'admin-product-manage'  : ['./src/page/admin-page/product-manage/index.js'],
        'admin-category-manage' : ['./src/page/admin-page/category-manage/index.js'],
        'admin-order-manage'    : ['./src/page/admin-page/order-manage/index.js'],
        'admin-index'           : ['./src/page/admin-page/index/index.js'],
        'attendance'            : ['./src/page/attendance/index.js'],
        'attendance-create'     : ['./src/page/attendance-create/index.js']
    },
    output : {
        path        : __dirname + '/dist',
        publicPath  : '/dist',
        filename    : 'js/[name].js'
    },
    // 代理配置
    devServer: {
        proxy: {
            '/supperDistribution/*': {
                target: 'http://localhost:8080/',
                secure: false
            }
        }
    },
    externals: {
        jquery : 'window.jQuery'
    },
    module : {
        loaders : [
            {test : /\.css$/, loader : ExtractTextPlugin.extract({fallbackLoader : 'style-loader', loader : 'css-loader'})},
            {test : /\.(gif|png|jpg|woff|svg|eot|ttf)\??.*$/, loader: 'url-loader?limit=100&name=/resource/[name].[ext]'},
            {
                test : /\.string$/, 
                loader : 'html-loader',
                //不让它删除string文件的引号，造成value=/的问题
                /*query : {
                    minimize : true,
                    removeAttributeQuotes : false
                }*/
            }
        ]
    },
    //配置路径别名
    resolve : {
        alias : {
            util            : __dirname + '/src/util',
            page            : __dirname + '/src/page',
            service         : __dirname + '/src/service',
            image           : __dirname + '/src/image',
            node_modules    : __dirname + '/node_modules'
        }
    },
    plugins : [
        //独立通用模块
        new webpack.optimize.CommonsChunkPlugin({
            name        : 'common',
            filename    : 'js/base.js'
        }),
        //css单独打包
        new ExtractTextPlugin("css/[name].css"),
        //html模板的处理
        new HtmlWebpackPlugin(getHtmlConfig('index', '首页')),
        new HtmlWebpackPlugin(getHtmlConfig('list', '商品列表')),
        new HtmlWebpackPlugin(getHtmlConfig('user-login', '用户登录')),
        new HtmlWebpackPlugin(getHtmlConfig('user-register', '免费注册')),
        new HtmlWebpackPlugin(getHtmlConfig('user-pass-reset', '找回密码')),
        new HtmlWebpackPlugin(getHtmlConfig('user-pass-update', '修改密码')),
        new HtmlWebpackPlugin(getHtmlConfig('user-center', '个人中心')),
        new HtmlWebpackPlugin(getHtmlConfig('user-center-update', '修改信息')),
        new HtmlWebpackPlugin(getHtmlConfig('result', '操作结果')),
        new HtmlWebpackPlugin(getHtmlConfig('about', '关于我们')),
        new HtmlWebpackPlugin(getHtmlConfig('admin-user-login', '管理登录')),
        new HtmlWebpackPlugin(getHtmlConfig('admin-product-manage', '商品管理')),
        new HtmlWebpackPlugin(getHtmlConfig('admin-category-manage', '品类管理')),
        new HtmlWebpackPlugin(getHtmlConfig('admin-order-manage', '订单管理')),
        new HtmlWebpackPlugin(getHtmlConfig('admin-index', '管理系统')),

        new HtmlWebpackPlugin(getHtmlConfig('attendance', '打卡信息管理')),
        new HtmlWebpackPlugin(getHtmlConfig('attendance-create', '声纹创建'))
    ]
};

//如果是dev开发模式，就把更改刷新服务器放进去，如果是online线上则不会刷新
if('dev' == WEBPACK_ENV){
    config.entry.common.push('webpack-dev-server/client?http://localhost:8088/');
}
module.exports = config;